(in-package :lilu-x11)

(defun xclip-copy-string-to (string &key (selection :clipboard))
  (assert (member selection '(:clipboard :primary :secondary)))
  (let ((cmd (format nil "xclip -selection ~A -t UTF8_STRING -i " (string-downcase (symbol-name selection)))))
    (with-input-from-string (input-stream
                             (coerce string 'string))
      (uiop:run-program cmd :output nil :input input-stream))))

(defun xclip-paste-string-from (&key (selection :clipboard) (ignore-error-status nil))
  (assert (member selection '(:clipboard :primary :secondary)))
  (let ((cmd (format nil "xclip -selection ~A -t UTF8_STRING -o " (string-downcase (symbol-name selection)))))
    (multiple-value-bind (output error status)
        (uiop:run-program cmd :output :string :error-output :string :ignore-error-status ignore-error-status)
      (when (eql status 1)
        (format *debug-io* "xclip error (status=1) ~A" error))
      (unless (eql status 1)
        output))))
